package main

import (
	"fmt"
	"go_gin_bubble/dao"
	"go_gin_bubble/models"
	"go_gin_bubble/routers"
	"go_gin_bubble/setting"
	"os"
)

func main(){

	//配置
	if len(os.Args) < 2{
		fmt.Println("Usage: ./go_bubble conf/config.ini")
		return 
	}
	//加载配置为文件
	if err:= setting.Init(os.Args[1]);err!=nil {
		// fmt.Printf("load config from file failed, err:#{err}\n")
		fmt.Printf("load config from file failed, err:%v\n", err)
		return 
	}

	//创建数据库
	err := dao.InitMySQL(setting.Conf.MySQLConfig)
	if err!=nil {
		// fmt.Printf("mysql init failed, err:#{err}\n")
		fmt.Printf("init mysql failed, err:%v\n", err)
		return 
	}
	defer dao.CloseDB()

	//模型绑定
	dao.DB.AutoMigrate(&models.Todo{})

	//路由注册
	router := routers.SetupRouter()
	if err := router.Run(fmt.Sprintf(":%d", setting.Conf.Port)); err != nil {
		fmt.Printf("server startup failed, err:%v\n", err)
	}
}